package com.zzyk.main.service.impl;

import cn.hutool.core.date.DateUtil;
import com.zzyk.main.mapper.DeviceUserateMapper;
import com.zzyk.main.model.pojo.DeviceUserate;
import com.zzyk.main.model.vo.KeyValueTwo;
import com.zzyk.main.service.DeviceUserateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DeviceUserateServiceImpl implements DeviceUserateService{

    @Resource
    private DeviceUserateMapper deviceUserateMapper;

    @Override
    public int insertSelective(DeviceUserate record) {
        return deviceUserateMapper.insertSelective(record);
    }

  /*  @Override
    @Transactional
    public Result calculateAverageUsage() {
        try {
        List<Device> deviceList = deviceMapper.selectAllByType(1, "",null, areaArr);
        for (Device device : deviceList) {
            *//*获取当前设备今天需要工作多长时间*//*
            *//*获取设备所有关联的时间规则*//*
            String week = DateUtils.getWeek();
            List<TimeRule> list = timeRuleMapper.selectWeekTime(week,device.getEnterpise());
            *//*得到今天应该工作多少分钟*//*
            long betweenDay =0;
            for (TimeRule timeRule : list) {
                betweenDay += DateUtil.between(DateUtil.parse(timeRule.getStartTime()), DateUtil.parse(timeRule.getEndTime()), DateUnit.MINUTE);
            }
            if(betweenDay==0){
                return Result.ok("0");
            }
            *//*根据设备求今天工作多长时间 分钟*//*
            String productionTime = deviceExtraDataMapper.selectTodayProductionTime(device.getId());
            productionTime= StringUtils.isBlank(productionTime)?"0":productionTime;
            double div = CalculateUtils.div(Double.parseDouble(productionTime), betweenDay, 3);
            DeviceUserate deviceUserate =new DeviceUserate();
            deviceUserate.setDeviceId(device.getId());
            deviceUserate.setUsageRate(div * 100);
            deviceUserateMapper.insertSelective(deviceUserate);
        }
        return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("失败");
        }
    }
*/
    @Override
    public List<DeviceUserate> findDeviceUserateBuDeviceId(Integer id,String beginTime,String endTime,Integer type) {
        Date  bDate=StringUtils.isBlank(beginTime)?DateUtil.lastWeek():DateUtil.parse(beginTime);
        Date  eDate=StringUtils.isBlank(endTime)?DateUtil.offsetDay(DateUtil.date(),-1):DateUtil.parse(endTime);
        if(type==1){
              bDate=StringUtils.isBlank(beginTime)?DateUtil.offsetDay(DateUtil.lastMonth(),-1):DateUtil.parse(beginTime);
        }
        return   deviceUserateMapper.selectAllByDeviceId(id,bDate,eDate);
    }

    @Override
    public List<DeviceUserate> getProductionReport(String dateTime, Integer enterprise, Integer[] deviceList) {
        String beginDate =  DateUtil.formatDate(DateUtil.offsetDay(new Date(),-29));
        String endDate = DateUtil.today();
        if (StringUtils.isNotBlank(dateTime)) {
            String[] split = dateTime.split(" ~ ");
            beginDate = split[0];
            endDate = split[1];
        }
        return deviceUserateMapper.selectAllByEnterprise(beginDate,endDate,enterprise,deviceList);
    }

    @Override
    public List<KeyValueTwo> getDeviceTypeUtilizationRate(String dateTime, Integer enterprise, Integer[] deviceList) {
        String beginDate =  DateUtil.formatDate(DateUtil.offsetDay(new Date(),-29));
        String endDate = DateUtil.today();
        if (StringUtils.isNotBlank(dateTime)) {
            String[] split = dateTime.split(" ~ ");
            beginDate = split[0];
            endDate = split[1];
        }
        return deviceUserateMapper.selectDeviceTypeUtilizationRate(beginDate,endDate,enterprise,deviceList);
    }

    @Override
    public List<Map<Object, Object>> getAnnualUtilizationRate(String year, Integer enterprise, Integer[] deviceList) {
        if(StringUtils.isBlank(year)){
            year=DateUtil.format(new Date(),"yyyy");
        }
        System.out.println("year = " + year);

        return deviceUserateMapper.selectAnnualUtilizationRate(year,enterprise,deviceList);
    }
}
